var laydate;
var form;
layui.use(['form','laydate'],function(){
    form = layui.form,
    laydate = layui.laydate,


//显示工作流程数据
$(function(){
    $.ajax({
        url:"../../api/work-time.json",
        success:function(data){
            let Work=JSON.stringify(data.data);
            localStorage.setItem("myWork",Work);
        }
    });
    //显示数据
    workInit();
});


//展示数据
function workInit(){
    //json数据
    let mywork=JSON.parse(localStorage.getItem("myWork"));
    //循环数据
    one:
    for(let i=0;i<mywork.length;i++){
      //拿登录员工的id
      let tId=JSON.parse(localStorage.getItem("tId"));
      two:
      for(let j=0;j<tId.length;j++){
          let loginId=tId[0].id;
          // console.log(loginId);  //拿到当前员工的id
          if(loginId==mywork[i].id){
              //文本框显示的内容
              form.val("changes",{
                "time1":mywork[i].time1,
                "work1":mywork[i].work1,
                "time2":mywork[i].time2,
                "work2":mywork[i].work2,
                "time3":mywork[i].time3,
                "work3":mywork[i].work3,
                "time4":mywork[i].time4,
                "work4":mywork[i].work4,
                "time5":mywork[i].time5,
                "work5":mywork[i].work5
              });
              break one;
          }
      }
    }
}

//修改工作
$("#test1").on("click",function(){
    //显示弹框
    $("#msg").css("display","block");
    //给input框添加日期事件
    laydate.render({
        elem: '#time-box'
        ,type: 'time'
        ,range: '至'
        ,format: 'H:m:s'
    });
    //获取原来框的值
    let data = form.val('changes');
    //将数据显示到弹框里
    form.val("changes",{
        "time-content":data.time1,
        "work-content":data.work1
    })
});

//修改
$("#adp").on("click",function(){
    //获取本地存储的数据
    let mywork1=JSON.parse(localStorage.getItem("myWork"));
    // console.log(mywork1[0].id)
    //拿到表单元素的值
    let time=$("#time-box").val();
    let work=$("#work-box").val();
    // console.log(time+work)
    //拿登录员工的id
    let tId=JSON.parse(localStorage.getItem("tId"));
    let loginId=tId[0].id;
    //循环数据
    for(let i=0;i<mywork1.length;i++){
        if(loginId==mywork1[i].id){
            console.log(11)
            mywork1[i].time1=time;
            mywork1[i].work1=work;
        }
    }
    //将最新的数据保存在本地
    localStorage.setItem("myWork",JSON.stringify(mywork1));
     //刷新数据
     workInit();
     //弹框消失
    $("#msg").css("display","none");
});

//取消修改
$("#giveUp").on("click",function(){
    //弹框消失
    $("#msg").css("display","none");
})

//添加事件
$("#append").on("click",function(){
    //显示弹框
    $("#add").css("display","block");
    //给input框添加日期事件
    laydate.render({
        elem: '#time-box1'
        ,type: 'time'
        ,range: '至'
        ,format: 'H:m:s'
    });
})

//添加
$("#adp1").on("click",function(){
    //获取本地存储的数据
    let mywork1=JSON.parse(localStorage.getItem("myWork"));
    // console.log(mywork1[0].id)
    //拿到表单元素的值
    let time=$("#time-box1").val();
    let work=$("#work-box1").val();
    //添加节点
    $("hr").before(`<li class="layui-timeline-item">
    <i class="layui-icon layui-timeline-axis"></i>
    <div class="layui-timeline-content layui-text">
      <form class="layui-form" action="#" lay-filter="changes">
          <div class="layui-form-item">
              <div class="layui-input-inline">
                  <input type="text" class="layui-input" name="time5" placeholder="请选择时间范围" readonly>
              </div>
          </div>

          <div class="layui-form-item layui-form-text">
              <div class="layui-input-inline">
              <textarea name="work5" placeholder="请输入工作内容" class="layui-textarea" readonly></textarea>
              <div class="layui-btn-container">
                  <button class="layui-btn" id="test4" onclick="return false">修改工作</button>
              </div>
              </div>
          </div>
      </form> 
    </div>
  </li>`)
    //拿登录员工的id
    let tId=JSON.parse(localStorage.getItem("tId"));
    let loginId=tId[0].id;
    //循环数据
    for(let i=0;i<mywork1.length;i++){
        if(loginId==mywork1[i].id){
            console.log(11)
            mywork1[i].time5=time;
            mywork1[i].work5=work;
        }
    }
    //将最新的数据保存在本地
    localStorage.setItem("myWork",JSON.stringify(mywork1));
     //刷新数据
     workInit();
     //弹框消失
    $("#add").css("display","none");
})

//取消添加
$("#giveUp1").on("click",function(){
    //弹框消失
    $("#add").css("display","none");
})

});